package com.briup.estore.service;

import com.briup.estore.bean.Order;
import com.briup.estore.bean.OrderItem;
import com.briup.estore.bean.Shopcar;
import com.briup.estore.bean.ext.OrderExtend;
import com.briup.estore.dto.OrderCreateDTO;
import com.briup.estore.dto.OrderPaySuccessDTO;
import com.briup.estore.dto.OrderSearchDTO;
import com.briup.estore.mapper.BookMapper;
import com.briup.estore.mapper.OrderItemMapper;
import com.briup.estore.mapper.OrderMapper;
import com.briup.estore.mapper.ShopcarMapper;
import com.briup.estore.utils.BeanCopyUtils;
import com.briup.estore.vo.OrderInfoVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

@Service
public class OrderService {
    @Resource
    private OrderMapper orderMapper;
    @Resource
    private ShopcarMapper shopcarMapper;
    @Resource
    private OrderItemMapper orderItemMapper;
    @Resource
    private BookMapper bookMapper;

    @Transactional
    public void createOrder(OrderCreateDTO orderCreateDTO, int userId) {
//      复制为订单(mapper处理该类)
        Order order = BeanCopyUtils.copyBean(orderCreateDTO, Order.class);
//      设置订单的用户(将来添加使用)
        order.setUserId(userId);
        orderMapper.saveOrder(order); // 添加订单
//        2.添加订单项 一个订单有多个订单项   购物车的每行就是一个订单项 21 25
        List<Integer> shopCarIds = orderCreateDTO.getShopCarIds();
        for (Integer id : shopCarIds) {
            // 2.1 查出每一行的购物车
            Shopcar shopcar = shopcarMapper.findById(id);
            // 2.2 将每行的购物车 转化为 订单项
            OrderItem orderItem = BeanCopyUtils.copyBean(shopcar, OrderItem.class);
            orderItem.setOrderId(order.getId());
            // 2.3 添加订单项
            orderItemMapper.saveOrderItem(orderItem);
            // 3.移除购物车
            shopcarMapper.deleteById((id));
            // 4.减库存
            bookMapper.updateStoreNum(orderItem.getBookId(), orderItem.getNum());
        }
    }

    public List<OrderInfoVO> getOrders(int userId, OrderSearchDTO
            orderSearchDTO) {
        List<OrderExtend> list =
                orderMapper.selectOrderDetailByUserId(userId, orderSearchDTO);
        list.forEach(OrderExtend::genPrice);
        List<OrderInfoVO> vos = BeanCopyUtils.copyBeanList(list, OrderInfoVO.class);
        return vos;
    }
    public void paySuccess(OrderPaySuccessDTO paySuccessDTO) {
        orderMapper.paySuccess(paySuccessDTO);
    }
}
//    public List<OrderInfoVO> ordlist(String userID) {
////        订单表
////        用户表
////        订单项（计算所有订单数量和价格）
////        书籍表
//        List<OrderExtend> orderExtends = orderMapper.orderlist(Integer.parseInt(userID));
////       2,计算价格和总数
//        for (OrderExtend orderExtend : orderExtends){
//            orderExtend.getTotalPrice();
//        }
////        3,最终返回List<OrderInfoVO>
//        List<OrderInfoVO> orderInfoVOS = BeanCopyUtils.copyBeanList(orderExtends, OrderInfoVO.class);
//        return orderInfoVOS;
//    }
//}